﻿using System;
using System.Data;
using ltModel;
using Telerik.Web.UI;

public partial class ltMan_news_clsAdd : AdminBasePage
{
    ltModel.BLL.article_class acbl = new ltModel.BLL.article_class();

    protected void Page_Load(object sender, EventArgs e)
    {
        if (!IsPostBack)
        {
            RadTreeView treeview = clsParent.Items[0].FindControl("RadTreeView1") as RadTreeView;
            BindTreeView(treeview);

            string id = Request.QueryString["id"];
            if (Str.IsInt(id))
            {
                ltModel.Model.article_class acbo = acbl.GetModel(int.Parse(id));
                clsName.Text = acbo.classname;
                RadTreeNode node = treeview.FindNodeByValue(acbo.father_id.ToString());
                node.Selected = true;
            }
        }
    }
    void BindTreeView(RadTreeView treeview)
    {
        treeview.DataSource = SqlHelper.Instance.ExecuteReader("select '0' as class_id,'一级类别' as classname,'0' as father_id union all select class_id,classname,father_id from lt_article_class");
        treeview.DataBind();
        treeview.ExpandAllNodes();
    }
    protected void RadSave_Click(object sender, EventArgs e)
    {
        string id = Request.QueryString["id"];
        bool inEdit = Str.IsInt(id);
        ltModel.Model.article_class acbo = null;
        if (inEdit)
            acbo = acbl.GetModel(int.Parse(id));
        else
            acbo = new ltModel.Model.article_class();

        if (!inEdit || clsName.Text != acbo.classname)
        {
            if (acbl.GetList("classname='" + clsName.Text.Trim() + "' and father_id  =" + clsParent.SelectedValue).Tables[0].Rows.Count > 0)
            {
                infoPanel.AddErrorInfo("此类别名称已存在！");
                OperateLog.AddFailedLog(CurrentUser.user_id, Request.UserHostAddress, "类别管理-添加>类别名称[" + clsName.Text + "]已存在");
                return;
            }
        }

        acbo.classname = DataProtector.ToSBC(clsName.Text.Trim());//类别名
        acbo.father_id = Int32.Parse(clsParent.SelectedValue);//父类ID

        string pic = clsPic.SaveFiles();
        if (!string.IsNullOrEmpty(pic))
            acbo.beizhu = pic;

        acbo.model_id = 28;//模板
        acbo.sub_class = "0";//类别属性
        acbo.px = acbl.MaxPx();
        acbo.ceng_num = "0";
        if (acbo.father_id > 0)
        {
            DataTable dt = acbl.GetAllList().Tables[0];
            for (int i = 0; i < dt.Rows.Count; i++)
            {
                if (dt.Rows[i]["Class_id"].ToString() == acbo.father_id.ToString())
                {
                    acbo.px = acbl.UpdateClassPx(Int32.Parse(dt.Rows[i]["Class_id"].ToString()), Int32.Parse(dt.Rows[i]["Px"].ToString()));//修改排序,新序号
                    acbl.UpdateSubClass(1, acbo.father_id);//修改类别属性(是否有子类[1])
                    acbo.ceng_num = dt.Rows[i]["ceng_num"].ToString() + "|" + acbo.father_id.ToString();//类别关系
                    //acbo.Px=Int32.Parse(cdt.Rows[i]["Px"].ToString())+1;//排序
                    i = dt.Rows.Count;
                }
            }
        }

        if (!inEdit)
        {
            acbl.Add(acbo);
            OperateLog.AddSuccessLog(CurrentUser.user_id, Request.UserHostAddress, "类别管理-添加>" + acbo.classname);
        }
        else
        {
            acbl.Update(acbo);
            OperateLog.AddSuccessLog(CurrentUser.user_id, Request.UserHostAddress, "类别管理-修改>" + acbo.classname);
        }

        infoPanel.AddSuccessInfo(inEdit ? "修改成功" : "添加成功");
        AddScriptM("setTimeout(parent.reloadData, 300);");
    }
}